<template>
  <div class="login clearfix">
    <div class="login-wrap">
      <el-row type="flex" justify="center">
        <el-form ref="loginForm" :model="user" status-icon label-width="80px">
          <h3>注册</h3>
          <hr>
          <el-form-item prop="username" label="用户名">
            <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="phone" label="手机号">
            <el-input v-model="user.phone" placeholder="请输入手机号"></el-input>
          </el-form-item>
          <el-form-item prop="password" label="设置密码">
            <el-input v-model="user.password" show-password placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon @click="doRegister()">注册账号</el-button>
          </el-form-item>
        </el-form>
      </el-row>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "login",
  data() {
    return {
      user: {
        username: "",
        phone: "",
        password: ""
      },
      resp:[],
    };
  },
  created() {

  },
  methods: {
    // 注册检查函数
    doRegister() {
      if (!this.user.username) {
        this.$message.error("请输入用户名！");
        return;
      } else if (!this.user.phone) {
        this.$message.error("请输入手机号！");
        return;
      } else if (this.user.phone != null) {
        // 设置手机号的正则
        var reg = /^1(3\d|4[5-8]|5[0-35-9]|6[567]|7[01345-8]|8\d|9[025-9])\d{8}$/;
        var reg2 = /\w{6,12}/;
        if (!reg.test(this.user.phone)) {
          this.$message.error("请输入有效的手机号！");
        } else if (!reg2.test(this.user.password)) {
          this.$message.error("请输入至少6位密码！");
          return;
        } else {
          // this.$router.push({ path: "/" }); //无需向后台提交数据，方便前台调试
          axios
              .post("/main/register/", {
                name: this.user.username,
                phone: this.user.phone,
                password: this.user.password
              })
              .then(res => {
                this.resp = res.data;
                // console.log("输出res.data", res.data);
                // console.log("输出账号", JSON.stringify(res));
                if (this.resp.code === 200) {
                  // 终于搞定了
                  alert("你的注册账号是："+this.resp["data"]);
                  this.$router.push({ path: "/login" });
                } else {
                  // 弹窗警告框：后端返回的信息
                  alert(this.resp["message"]);
                  return;
                }
              });
        }
      }
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.login {
  width: 100%;
  height: 740px;
  background: url("@/assets/03.webp") no-repeat;
  background-size: cover;
  overflow: hidden;
}
.login-wrap {
  /*background: url("../assets/images/login_bg.png") no-repeat;*/
  background-size: cover;
  width: 400px;
  height: 300px;
  margin: 215px auto;
  overflow: hidden;
  padding-top: 10px;
  line-height: 20px;
}

h3 {
  color: #0babeab8;
  font-size: 24px;
}
hr {
  background-color: #444;
  margin: 20px auto;
}

.el-button {
  width: 80%;
  margin-left: -50px;
}
</style>